//
//  TableBar.m
//  PetApp
//
//  Created by apple on 16/3/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "TableBar.h"
#import "HomeViewController.h"
#import "ShopVIewController.h"
#import "TabbarCustom.h"
#import "TabBarItem.h"


@interface TableBar () <TabbarCustomDelegate>

@property (nonatomic, copy) void(^tabBarBlock)();

@property (nonatomic, retain) UIImageView *imageViewOfCamera;

@end

@implementation TableBar

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubViews];
    

}

- (void)createSubViews {
    
    
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    UINavigationController *homeOfNavi = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeVC.edgesForExtendedLayout = UIRectEdgeNone;
    [homeVC release];
    
    ShopVIewController *shopVC = [[ShopVIewController alloc] init];
    UINavigationController *shopOfNavi = [[UINavigationController alloc] initWithRootViewController:shopVC];
    [shopVC release];
    
    self.viewControllers = @[homeOfNavi, shopOfNavi];
    [shopOfNavi release];
    [homeOfNavi release];
    /** 这两行是隐藏tabbar */
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

    TabbarCustom *tabBar = [[TabbarCustom alloc] initWithFrame:self.tabBar.bounds];
    [self.tabBar addSubview:tabBar];
    [tabBar release];
    
    CGFloat buttonWidth = [UIScreen mainScreen].bounds.size.width / 3;
    CGFloat tabBarHeight = CGRectGetHeight(tabBar.frame);
    
    TabBarItem *homeItem = [self tabBarItemWithFrame:CGRectMake(0, 0, buttonWidth, tabBarHeight) title:@"首页" normalImageName:@"Home30x30" selectedImageName:@"HomeSelect30x30" tabBarItemType:TabBarItemNormal];
    TabBarItem *cameraItem = [self tabBarItemWithFrame:CGRectMake(buttonWidth, 0, buttonWidth, tabBarHeight) title:@"发布" normalImageName:@"post_normal" selectedImageName:@"post_normal" tabBarItemType:TabBarItemBulge];
    TabBarItem *shopItem = [self tabBarItemWithFrame:CGRectMake(buttonWidth * 2, 0, buttonWidth, tabBarHeight) title:@"乐购" normalImageName:@"Shop30x30" selectedImageName:@"ShopSelect30x30" tabBarItemType:TabBarItemNormal];
    
    tabBar.delegate = self;
    tabBar.tabBarItems = @[homeItem, cameraItem, shopItem];
    
    
}

- (TabBarItem *)tabBarItemWithFrame:(CGRect)frame title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName tabBarItemType:(TabBarItemType)tabBarItemType {
    TabBarItem *item = [[TabBarItem alloc] initWithFrame:frame];
    [item setTitle:title forState:UIControlStateNormal];
    [item setTitle:title forState:UIControlStateSelected];
    item.titleLabel.font = [UIFont systemFontOfSize:8];
    UIImage *normalImage = [UIImage imageNamed:normalImageName];
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    [item setImage:normalImage forState:UIControlStateNormal];
    [item setImage:selectedImage forState:UIControlStateSelected];
    [item setImage:selectedImage forState:UIControlStateHighlighted];
    [item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1] forState:UIControlStateNormal];
    [item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1] forState:UIControlStateSelected];
    item.tabBarItemType = tabBarItemType;
    
    return item;
}

#pragma mark - TabBarDelegate

- (void)tabBarDidSelectedBulgeButton {
    self.tabBarBlock();

}

- (void)handleVisualViewWithBlock:(void (^)())block {
    self.tabBarBlock = block;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
